package com.company.libraryservice.controller;

import com.company.libraryservice.common.ApiResp;
import com.company.libraryservice.model.Book;
import com.company.libraryservice.model.BorrowRecord;
import com.company.libraryservice.service.BookService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 图书 REST 控制器
 *
 * @author RoyMa
 * @date 2025-08-14
 */
@RestController
@RequestMapping("/books")
@RequiredArgsConstructor
public class BookController {
  private final BookService bookService;

  @GetMapping
  public ApiResp<List<Book>> list() {
    return ApiResp.ok(bookService.list());
  }

  @PostMapping
  public ApiResp<Book> add(@RequestBody Book book) {
    bookService.add(book);
    return ApiResp.ok(book);
  }

  @PostMapping("/{id}/borrow")
  public ApiResp<Void> borrow(@PathVariable Long id) {
    bookService.borrowBook(id, "zhangsan");
    return ApiResp.ok(null);
  }

  @PostMapping("/{id}/return")
  public ApiResp<Void> ret(@PathVariable Long id) {
    bookService.returnBook(id, "zhangsan");
    return ApiResp.ok(null);
  }

  @GetMapping("/records")
  public ApiResp<List<BorrowRecord>> records() {
    return ApiResp.ok(bookService.listRecords());
  }
}
